package com.ihk.toiletfinder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.ihk.model.Parser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class SplashActivity extends Activity implements Runnable {
	
	Handler handler;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
		
        File xml = new File(Parser.getFilePath());
		if(!xml.exists()) {
			try {
				File path = new File(Parser.getPath());
				path.mkdir();
				xml.createNewFile();
			} catch (IOException e) {
				Log.d("CreateFileError", e.getMessage());
			}
			
			try {
				Parser.getInstance().parse(getResources().openRawResource(R.raw.kobenhavn));
			} catch (Exception e) {
				Log.d("ParseFileError", e.getMessage());
			}
			
			Parser.saveToiletsAsXML();
		}
		else {
			try {
				Parser.getInstance().parse(new FileInputStream(Parser.getFilePath()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
        
		handler = new Handler();		
		handler.postDelayed(this, 1500);
    }

	@Override
	public void run() {
		Log.d("RUN", "RUN was executed!");
		SplashActivity.this.startActivity(new Intent(SplashActivity.this, MainActivity.class));
		finish();
	}

    
}
